package com.yuwubao.hz_International.controllers.sys;

import com.yuwubao.hz_International.entities.ProceedsConfigEntity;
import com.yuwubao.hz_International.entities.vo.RechargeVo;
import com.yuwubao.hz_International.services.AcountDetailService;
import com.yuwubao.hz_International.services.ConsumerService;
import com.yuwubao.hz_International.services.ProceedsConfigService;
import com.yuwubao.hz_International.util.Const;
import com.yuwubao.hz_International.util.PageUtil;
import com.yuwubao.hz_International.util.RestApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyu on 2018/1/26.
 */
@RestController
@RequestMapping("/sys/rechargeManage")
@Transactional
@CrossOrigin
@Api(value = "后台接口")
public class RechargeManageController {

    private final static Logger logger = LoggerFactory.getLogger(RechargeManageController.class);

    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private ProceedsConfigService proceedsConfigService;

    @Autowired
    private AcountDetailService acountDetailService;

    /**
     * 查询充值列表
     * @param consumerName  查询的用户名
     * @param beginTime  开始时间
     * @param endTime  结束时间
     * @param index  第几页
     * @param size  每页几条
     * @return
     */
    @GetMapping("/findRechargeList")
    @ApiOperation(value = "查询充值列表")
    public RestApiResponse<PageUtil<RechargeVo>> findRechargeList(@ApiParam(required = false, value = "用户名")@RequestParam(defaultValue = "", required = false) String consumerName,
                                                                  @ApiParam(required = false, value = "开始时间")@RequestParam(required = false, defaultValue = "")String beginTime,
                                                                  @ApiParam(required = false, value = "结束时间")@RequestParam(required = false, defaultValue = "")String endTime,
                                                                  @ApiParam(required = false, value = "第几页")@RequestParam(defaultValue = "1", required = false)int index,
                                                                  @ApiParam(required = false, value = "每页几条")@RequestParam(defaultValue = "10", required = false)int size) {
        RestApiResponse<PageUtil<RechargeVo>> result = new RestApiResponse<PageUtil<RechargeVo>>();
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);

            //添加分页
            PageUtil pageUtil = new PageUtil();
            pageUtil.setIndex(index);
            pageUtil.setSize(size);

            List<RechargeVo> list = acountDetailService.findRechargeList(map, pageUtil, consumerName);
            Integer rechargeDetailsSum = acountDetailService.findRechargeDetailsSum(map, consumerName);
            pageUtil.setTotalElements(rechargeDetailsSum);
            pageUtil.setTotalPages(size, rechargeDetailsSum);
            pageUtil.setList(list);
            result.successResponse(Const.SUCCESS, pageUtil);
        } catch (Exception e) {
            logger.warn("查询充值记录列表异常", e);
            result.failedApiResponse(Const.FAILED, "查询充值记录列表异常");
        }
        return result;
    }

    /**
     * 获取收款码配置
     * @return
     */
    @GetMapping("/getProceedsConfig")
    @ApiOperation(value = "获取收款码配置")
    public RestApiResponse<ProceedsConfigEntity> getProceedsConfig() {
        RestApiResponse<ProceedsConfigEntity> result = new RestApiResponse<ProceedsConfigEntity>();
        try {
            List<ProceedsConfigEntity> proceedsConfigEntityList = proceedsConfigService.findAll();
            if (proceedsConfigEntityList.size() > 0) {
                result.successResponse(Const.SUCCESS, proceedsConfigEntityList.get(0));
            }
        } catch (Exception e) {
            logger.warn("获取收款码配置异常", e);
            result.failedApiResponse(Const.FAILED, "获取收款码配置异常");
        }
        return result;
    }

    /**
     * 添加收款码配置
     * @param proceedsConfig
     * @return
     */
    @PostMapping("/addProceedsConfig")
    @ApiOperation(value = "添加收款码配置")
    public RestApiResponse<ProceedsConfigEntity> addProceedsConfig(@RequestBody ProceedsConfigEntity proceedsConfig) {
        RestApiResponse<ProceedsConfigEntity> result = new RestApiResponse<ProceedsConfigEntity>();
        try {
            List<ProceedsConfigEntity> proceedsConfigEntityList = proceedsConfigService.findAll();
            ProceedsConfigEntity proceedsConfigEntity = null;
            if (proceedsConfigEntityList.size() == 0) {
                proceedsConfigEntity = proceedsConfigService.save(proceedsConfig);
            } else {
                ProceedsConfigEntity configEntity = proceedsConfigEntityList.get(0);
                proceedsConfig.setId(configEntity.getId());
                proceedsConfigEntity = proceedsConfigService.update(proceedsConfig);
            }
            if (proceedsConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "添加收款码配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, proceedsConfigEntity, "添加成功");
        } catch (Exception e) {
            logger.warn("添加收款码配置异常", e);
            result.failedApiResponse(Const.FAILED, "添加收款码配置异常");
        }
        return result;
    }

    /**
     * 修改收款码配置
     * @param proceedsConfig
     * @return
     */
    @PostMapping("/updateProceedsConfig")
    @ApiOperation(value = "修改收款码配置")
    public RestApiResponse<ProceedsConfigEntity> updateBannerImg(@RequestBody ProceedsConfigEntity proceedsConfig) {
        RestApiResponse<ProceedsConfigEntity> result = new RestApiResponse<ProceedsConfigEntity>();
        try {
            ProceedsConfigEntity proceedsConfigEntity = proceedsConfigService.update(proceedsConfig);
            if (proceedsConfigEntity == null) {
                result.failedApiResponse(Const.FAILED, "修改收款码配置失败");
                return result;
            }
            result.successResponse(Const.SUCCESS, proceedsConfigEntity, "修改成功");
        } catch (Exception e) {
            logger.warn("修改收款码配置异常", e);
            result.failedApiResponse(Const.FAILED, "修改收款码配置异常");
        }
        return result;
    }


}
